﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Songhay.Xml
{

    /// <summary>
    /// Static helper members for XML-related routines.
    /// </summary>
    public static partial class XObjectUtility
    {

        #region GetXNode() overloads:

        /// <summary>
        /// Returns an <see cref="System.Xml.Linq.XNode"/>
        /// based on the setQuery Expression toward the source document.
        /// </summary>
        /// <param name="set">
        /// The source <see cref="System.Xml.Linq.XNode"/> set.
        /// </param>
        /// <param name="setQuery">The XPath Expression.</param>
        public static XNode GetXNode(XNode set, string setQuery)
        {
            XNode node = null;

            if (set == null) return node;
            node = ((IEnumerable)set.XPathEvaluate(setQuery)).Cast<XNode>().FirstOrDefault();

            return node;
        }

        /// <summary>
        /// Returns an <see cref="System.Xml.Linq.XNode"/>
        /// based on the setQuery Expression toward the source document.
        /// </summary>
        /// <param name="set">
        /// The source <see cref="System.Xml.Linq.XNode"/> set.
        /// </param>
        /// <param name="setQuery">The XPath Expression.</param>
        /// <param name="resolver">
        /// The <see cref="System.Xml.IXmlNamespaceResolver"/>
        /// to use to resolve prefixes.
        /// </param>
        public static XNode GetXNode(XNode set, string setQuery, IXmlNamespaceResolver resolver)
        {
            XNode node = null;

            if(set == null) return node;
            node = ((IEnumerable)set.XPathEvaluate(setQuery, resolver)).Cast<XNode>().FirstOrDefault();

            return node;
        }

        #endregion

        #region GetXNodes() overloads:

        /// <summary>
        /// Returns a <see cref="System.Collections.Generic.IEnumerable&lt;XElement&gt;"/> set
        /// based on the setQuery Expression toward the source document.
        /// </summary>
        /// <param name="set">
        /// The source <see cref="System.Xml.Linq.XNode"/> set.
        /// </param>
        /// <param name="setQuery">The XPath Expression.</param>
        public static IEnumerable<XNode> GetXNodes(XNode set, string setQuery)
        {
            IEnumerable<XNode> nodes = null;

            if (set == null) return nodes;
            nodes = ((IEnumerable)set.XPathEvaluate(setQuery)).Cast<XNode>();

            return nodes;
        }

        /// <summary>
        /// Returns a <see cref="System.Collections.Generic.IEnumerable&lt;XElement&gt;"/> set
        /// based on the setQuery Expression toward the source document.
        /// </summary>
        /// <param name="set">
        /// The source <see cref="System.Xml.Linq.XNode"/> set.
        /// </param>
        /// <param name="setQuery">The XPath Expression.</param>
        /// <param name="resolver">
        /// The <see cref="System.Xml.IXmlNamespaceResolver"/>
        /// to use to resolve prefixes.
        /// </param>
        public static IEnumerable<XNode> GetXNodes(XNode set, string setQuery, IXmlNamespaceResolver resolver)
        {
            IEnumerable<XNode> nodes = null;

            if(set == null) return nodes;
            nodes = ((IEnumerable)set.XPathEvaluate(setQuery, resolver)).Cast<XNode>();

            return nodes;
        }

        #endregion
    }
}
